<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$data = array();
		$this->template->load("theme","front",$data);
		
	}
	function sigin()
	{
			$this->form_validation->set_message('required', '*(%s)');
			$this->form_validation->set_rules('card_code', 'required', 'trim|required|max_length[13]|callback_card_code_check');
		
			if($this->form_validation->run() == FALSE)
			{
				$this->template->load("theme","sigin");
			}
			else
			{ 	
				
				
				$this->db->where("card_code",$_POST["card_code"]);
				$cust = $this->db->get("registers")->result_array();
				redirect('welcome/get/'.$cust[0]["register_id"], 'location');
				
				//$this->db->insert("registers",$_POST);
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				//$this->fs->save_log();	
				//redirect('admin/categories', 'location');
				
			}
		
		
	}
	function card_code_check($code)
	{
		$this->db->where("card_code",$code);
		$cust = $this->db->get("registers")->result_array();
		
		if (!count($cust))
		{
			$this->form_validation->set_message('card_code_check', '*(ไม่พบเลขบัตรประจำตัวประชาชนนี้ในฐานข้อมูล)');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	
	}
	function get($id)
	{
		$this->db->where("register_id",$id);
		$data["cust"] = $this->db->get("registers")->result_array();;
		$this->template->load("theme","get",$data);
		
	}
	
	
	function card_code_check_dup($code)
	{
		
		$cust = $this->db->query("select count(*) as c from registers where card_code='$code'")->result_array();
		
		if ($cust[0]["c"])
		{
			$this->form_validation->set_message('card_code_check_dup', '*มีเลขบัตรประจำตัวประชาชนนี้ในฐานข้อมูลแล้ว');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	
	}
	function voucher2($id)
	{
		
		 $this->db->where("register_id",$id);
		$cust =  $this->db->get("registers")->result_array();
		 
		 $fistname = $cust[0]["firstname"];
		 $lastname = $cust[0]["lastname"]; 
		 
		$this->load->library('image_lib');
		$config['source_image'] = 'include/images/voucher.jpg';
		$config['wm_text'] = "$fistname                            $lastname";
		$config['wm_type'] = 'text';
		$config['wm_font_path'] = 'tahoma.ttf';
		$config['wm_font_size'] = '20';
		$config['quality'] =100;
		$config['wm_font_color'] = '#000000';
		$config['wm_vrt_alignment'] = 'top';
		$config['wm_hor_alignment'] = 'left';
		$config['wm_padding'] = '150';
		 $config['dynamic_output'] = TRUE;
		

$this->image_lib->initialize($config);

$this->image_lib->watermark();
	}
	function voucher($id)
	{
		
		 $this->db->where("register_id",$id);
		$cust =  $this->db->get("registers")->result_array();
		 
		 $fistname = $cust[0]["firstname"];
		 $lastname = $cust[0]["lastname"]; 
		//กำหนด Header ส่วนที่เกี่ยวข้อง
		include ('fpdf/adodb/adodb.inc.php');
		define('FPDF_FONTPATH','fpdf/fpdf/font/');
		require ('fpdf/fpdf/jpdf.inc.php');
		$pdf=new FPDF();
		 
		// เพิ่มฟอนต์ภาษาไทยเข้ามา ตัวธรรมดา กำหนด ชื่อ เป็น angsana
		$pdf->AddFont('angsana','','angsa.php');
		 
		// เพิ่มฟอนต์ภาษาไทยเข้ามา ตัวหนา  กำหนด ชื่อ เป็น angsana
		$pdf->AddFont('angsana','B','angsab.php');
		 
		// เพิ่มฟอนต์ภาษาไทยเข้ามา ตัวหนา  กำหนด ชื่อ เป็น angsana
		$pdf->AddFont('angsana','I','angsai.php');
		 
		// เพิ่มฟอนต์ภาษาไทยเข้ามา ตัวหนา  กำหนด ชื่อ เป็น angsana
		$pdf->AddFont('angsana','BI','angsaz.php');
		 
		//สร้างหน้าเอกสาร
		$pdf->AddPage();
		 
		// กำหนดฟอนต์ที่จะใช้  อังสนา ตัวธรรมดา ขนาด 12
		$pdf->SetFont('angsana','',12);
		// พิมพ์ข้อความลงเอกสาร
		$pdf->setXY( 10, 10  );
		$pdf->Image('include/images/voucher.jpg',3,0,198);
		//$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' , 'อังสนา ตัวธรรมดา ขนาด 12' ) );
		 
		//$pdf->SetFont('angsana','B',16);
		$pdf->SetFont('angsana','I',24);
		$pdf->setXY( 35,42  );
		$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' , $fistname )  );

		$pdf->setXY( 110,42 );
		$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' ,$lastname )  );
		
		$pdf->setXY( 160,85 );
		$pdf->SetFont('angsana','I',20);
		$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' ,"NO. ".$this->getcode(5)));
		
		/*
		$pdf->SetFont('angsana','I',24);
		$pdf->setXY( 10, 30  );
		$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' , 'อังสนา ตัวเอียง ขนาด 24' )  );
		 
		$pdf->SetFont('angsana','BI',32);
		$pdf->setXY( 10, 40  );
		$pdf->MultiCell( 0  , 0 , iconv( 'UTF-8','cp874' , 'อังสนา ตัวหนาเอียง ขนาด 32' )  );
		*/
		$this->db->query("update registers  set last_print_date = now() where register_id = $id");
		$pdf->Output();

		
		
		//$this->template->load("theme","voucher");
	}
	function _maping($query)
	{
		$values = array();
		$values = $query;
		$values["prefix_name"] = $this->_get_prefix_id($query["prefix_name"]);
		$values["province_id"] = $this->_get_province_id($query["province_name"]);
		$values["district_id"] = $this->_get_district_id($query["district"],$values["province_id"]);
		$values["sub_district_id"] = $this->_get_sub_district_id($query["sub_district"],$values["district_id"]);
		$values["zipcode"] ="";
		$values["email"] ="";
		
		$values["mobile"] ="";
		$values["zipcode2"] ="";
		$values["moo2"] ="";
		$values["home_street2"] ="";
		$values["soi2"] ="";
		$values["province_name2"] ="";
		$values["road2"] ="";
		
		$values["province_id2"] =0;
		$values["district_id2"] = 0;
		$values["sub_district_id2"] = 0;
		return $values;
	}
	function _get_province_id($value="")
	{
		
		$value = str_replace("จังหวัด", "", $value);
		
		$this->db->where("province_name",$value) ;
		
		$rows = $this->db->get("province")->result_array();
		if(count($rows))
		{
		return $rows[0]["province_id"];
		}
		else
		{
			return 0;
		}
	}
	function _get_district_id($value,$parent=0)
	{
		$value = str_replace("อำเภอ", "", $value);
		//$value = str_replace("เขต", "", $value);
		
		$this->db->where("amphur_name",$value) ;
		$this->db->where("province_id",$parent);
		$rows = $this->db->get("amphur")->result_array();
		if(count($rows))
		{
		return $rows[0]["amphur_id"];
		}
		else
		{
			return 0;
		}
		
	}
	function _get_sub_district_id($value,$parent)
	{
		$value = str_replace("ตำบล", "", $value);
		$value = str_replace("แขวง", "", $value);
		$this->db->where("district_name",$value) ;
		$this->db->where("amphur_id",$parent);
		$rows = $this->db->get("district")->result_array();
		if(count($rows))
		{
			return $rows[0]["district_id"];
		}
	}
	function _get_prefix_id($value)
	{
			if(("นาย"==$value)||("นางสาว"==$value)||("นาง"==$value))
			{
				return $value;
			}
			else
			{
				return "";
			}
			
	}
	function select_check($str)
	{
		
		if (!$str)
		{
			$this->form_validation->set_message('select_check', '[*]');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	
		
	}
	function check_id()
	{
	
	}
	function register($id=0)
	{
		if($id==1)
		{
			if(!$this->card_code_check_dup($_POST["card_code"]))
			{
				$this->db->where("card_code",$_POST["card_code"]);
				$cust = $this->db->get("registers")->result_array();
				redirect('welcome/get/'.$cust[0]["register_id"], 'location');
			}
			if(isset($_POST["photo"]))
			{
				 $this->db->where("card_code",$_POST["card_code"]);
				 $r =$this->db->get("card_photo")->result_array();
				if(!count($r))
				{
					$value = array("card_code"=>$_POST["card_code"],"photo"=>$_POST["photo"]);
					$this->db->insert("card_photo",$value);
				}
			}
			
		}
		$data = array();
		$cust = array();
		if($id)
		{
			$cust = $this->_maping( $_POST);
		}
		else
		{
			
			$cust["firstname"] ="";
			$cust["lastname"] ="";
			$cust["prefix_name"] ="";
			$cust["birth_day"]= "";
			$cust["birth_month"]="";
			$cust["birth_year"]="";
			$cust["sex"] ="";
			$cust["province"]="";
			$cust["disctrict"] ="";
			$cust["sub_distric"] ="";
			$cust["zip_code"] ="";
			$cust["moo"] ="";
			$cust["home_street"] ="";
			$cust["soi"] ="";
			$cust["province_name"] ="";
			$cust["road"] ="";
			$cust["card_code"] = "";
			$cust["province_id"] =0;
			$cust["district_id"] = 0;
			$cust["sub_district_id"] = 0;
			$cust["mobile"] = "";
			$cust["email"] ="";
			$cust["zipcode"] ="";
			
			$cust["zipcode2"] ="";
			$cust["moo2"] ="";
			$cust["home_street2"] ="";
			$cust["soi2"] ="";
			$cust["province_name2"] ="";
			$cust["road2"] ="";
			$cust["card_code2"] = "";
			$cust["province_id2"] =0;
			$cust["district_id2"] = 0;
			$cust["sub_district_id2"] = 0;
			$cust["full_address"] ="";
			$cust["sex"];
		}
		$data["cust"] =$cust;
		
			
			$this->form_validation->set_message('required', '[*]');
			$this->form_validation->set_rules('firstname',  'required', 'trim|required');
			$this->form_validation->set_rules('lastname', 'required', 'trim|required');
			$this->form_validation->set_rules('card_code', 'required', 'trim|required|max_length[13]|numeric|callback_card_code_check_dup');
			$this->form_validation->set_rules('home_street', 'required', 'trim|required');
			$this->form_validation->set_rules('moo', '', 'trim');
			$this->form_validation->set_rules('soi', '', 'trim');
			$this->form_validation->set_rules('road', '', 'trim');
			$this->form_validation->set_rules('prefix_name', '', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('zipcode', 'required', 'trim|required');
			if(!$id){
			$this->form_validation->set_rules('province_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('district_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('sub_district_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('province_id2', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('district_id2', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('sub_district_id2', 'required', 'trim|required|callback_select_check');
			
			}
			$this->form_validation->set_rules('full_address', '', 'trim');
			$this->form_validation->set_rules('sex', '', 'trim');
			$this->form_validation->set_rules('mobile', 'required', 'trim|required');
			$this->form_validation->set_rules('email', 'required', 'trim|valid_email');
			
			$this->form_validation->set_rules('home_street2', 'required', 'trim|required');
			$this->form_validation->set_rules('moo2', '', 'trim');
			$this->form_validation->set_rules('soi2', '', 'trim');
			$this->form_validation->set_rules('road2', '', 'trim');
			$this->form_validation->set_rules('zipcode2', 'required', 'trim|required');
			$this->form_validation->set_rules('same_address[]', 'required', '');
			$this->form_validation->set_rules('birth_day', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('birth_month', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('birth_year', 'required', 'trim|required|callback_select_check');
			
			
			$this->form_validation->set_rules('status', 'required', 'trim|required');
			
			$this->form_validation->set_rules('have_child', 'required', 'trim|required');
			$this->form_validation->set_rules('child_total', 'required', 'trim');
			
			$this->form_validation->set_rules('careers[]', 'required', 'trim|required');
			$this->form_validation->set_rules('product_types[]', 'required', 'trim|required');
			$this->form_validation->set_rules('purposies[]', 'required', 'trim|required');
			
			$this->form_validation->set_rules('sbase_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('buy_type_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('budget_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('mbase_id', 'required', 'trim|required|callback_select_check');
			

			
			
			
			if ($this->form_validation->run() == FALSE)
			{
			$this->template->load("theme","register",$data);
			}
			else
			{ 	
				
				$qeury =$_POST;
				unset($qeury["product_types"]);
				unset($qeury["careers"]);
				unset($qeury["purposies"]);
				unset($qeury["same_address"]);
				$qeury["at_station"] =1;
				$qeury["birth_year"] = ($qeury["birth_year"]-543);
				$qeury['create_date'] =date("y/m/d H:i:s");
		
				$result =$this->db->insert("registers",$qeury);
				
				if($result)
				{
					$m = $this->db->query("select max(register_id) as m from registers")->result_array();	
					
					foreach($_POST["careers"] as $row){
						
						$this->db->insert("register_career_relation",array("career_id"=>$row,"register_id" =>$m[0]["m"]));
					
					
					}
					
					foreach($_POST["product_types"] as $row){
						
						$this->db->insert("register_product_type_relation",array("product_type_id"=>$row,"register_id" =>$m[0]["m"]));
					
					
					}
					foreach($_POST["purposies"] as $row){
						
						$this->db->insert("register_purpose_relation",array("purpose_id"=>$row,"register_id" =>$m[0]["m"]));
					
					
					}
				}
				
				redirect('welcome/get/'.$m[0]["m"], 'location');
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				//$this->fs->save_log();	
				//redirect('admin/categories', 'location');
				
			}
	}
	function pdf($id)
	{
		
		$data["id"] = $id;
		$this->template->load("theme","pdf",$data);
	}
	function register_edit($id)
	{
		
		$data = array();
		
		
		
		
			
			$this->form_validation->set_message('required', '[*]');
			$this->form_validation->set_rules('firstname',  'required', 'trim|required');
			$this->form_validation->set_rules('lastname', 'required', 'trim|required');
			$this->form_validation->set_rules('card_code', 'required', 'trim|required|max_length[13]|numeric');
			$this->form_validation->set_rules('home_street', 'required', 'trim|required');
			$this->form_validation->set_rules('moo', '', 'trim');
			$this->form_validation->set_rules('soi', '', 'trim');
			$this->form_validation->set_rules('road', '', 'trim');
			$this->form_validation->set_rules('prefix_name', '', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('zipcode', 'required', 'trim|required');
			if(!$id){
			$this->form_validation->set_rules('province_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('district_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('sub_district_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('province_id2', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('district_id2', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('sub_district_id2', 'required', 'trim|required|callback_select_check');
			
			}
			$this->form_validation->set_rules('full_address', '', 'trim');
			$this->form_validation->set_rules('sex', '', 'trim');
			$this->form_validation->set_rules('mobile', 'required', 'trim|required');
			$this->form_validation->set_rules('email', 'required', 'trim|valid_email');
			
			$this->form_validation->set_rules('home_street2', 'required', 'trim|required');
			$this->form_validation->set_rules('moo2', '', 'trim');
			$this->form_validation->set_rules('soi2', '', 'trim');
			$this->form_validation->set_rules('road2', '', 'trim');
			$this->form_validation->set_rules('zipcode2', 'required', 'trim|required');
			$this->form_validation->set_rules('same_address[]', 'required', '');
			$this->form_validation->set_rules('birth_day', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('birth_month', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('birth_year', 'required', 'trim|required|callback_select_check');
			
			
			$this->form_validation->set_rules('status', 'required', 'trim|required');
			
			$this->form_validation->set_rules('have_child', 'required', 'trim|required');
			$this->form_validation->set_rules('child_total', 'required', 'trim');
			
			$this->form_validation->set_rules('careers[]', 'required', 'trim|required');
			$this->form_validation->set_rules('product_types[]', 'required', 'trim|required');
			$this->form_validation->set_rules('purposies[]', 'required', 'trim|required');
			
			$this->form_validation->set_rules('sbase_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('buy_type_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('budget_id', 'required', 'trim|required|callback_select_check');
			$this->form_validation->set_rules('mbase_id', 'required', 'trim|required|callback_select_check');
			

			
			
			
			if ($this->form_validation->run() == FALSE)
			{
				
				$this->db->where("register_id",$id);
				$cust = $this->db->get("registers")->result_array();
				$data["cust"] = $cust[0];
				$this->template->load("theme","register_edit",$data);
			}
			else
			{ 	
				
				$qeury =$_POST;
				$qeury["birth_year"] = ($qeury["birth_year"]-543);
				unset($qeury["product_types"]);
				unset($qeury["careers"]);
				unset($qeury["purposies"]);
				unset($qeury["same_address"]);
				//$qeury["at_station"] =1;
				$qeury['last_modify'] =date("y/m/d H:i:s");
				$this->db->where("register_id",$id);
				$result =$this->db->update("registers",$qeury);
				
				if($result)
				{
					
					
					$this->db->where("register_id",$id);
					$this->db->delete("register_career_relation");
					foreach($_POST["careers"] as $row){
						
						$this->db->insert("register_career_relation",array("career_id"=>$row,"register_id" =>$id));
					
					
					}
					$this->db->where("register_id",$id);
					$this->db->delete("register_product_type_relation");
					foreach($_POST["product_types"] as $row){
						
						$this->db->insert("register_product_type_relation",array("product_type_id"=>$row,"register_id" =>$id));
					
					
					}
					$this->db->where("register_id",$id);
					$this->db->delete("register_purpose_relation");
					foreach($_POST["purposies"] as $row){
						
						$this->db->insert("register_purpose_relation",array("purpose_id"=>$row,"register_id" =>$id));
					
					
					}
				}
				
				redirect('welcome/get/'.$id, 'location');
				
			}
	

	
	}
	function getcode($code)
        {
                
                
                $ucode = $this->db->query("select * from voucher_code where code ='$code'")->result_array();
                
                
                     $this->db->query("update voucher_code set run_no = run_no+1 where  code ='$code'");
						//echo  str_pad($ucode[0]['run_no'],4,0,STR_PAD_LEFT);
                        return str_pad($ucode[0]['run_no'],4,0,STR_PAD_LEFT);
               
                
        
                
        }
	
		
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */